import { queryMaterialDetail, batchUploadVideoAsync, searchMaterial, getCapInfo, getVideoDetail, deleteFolder, getFolderDetail, deleteMaterial } from '../../api/sysApi/material'
import {
	searchFolder,
	createFolder,
} from '../../api/product'
class MaterialController {
	static async queryMaterialDetail(ctx) {
		ctx.verifyParams({
			MaterialId: { type: 'string', required: true }
		})
		const res = await queryMaterialDetail({
			material_id: ctx.query.MaterialId,
			access_token: ctx.headers.access_token
		})
		ctx.body = res
	}
	static async searchMaterial(ctx) {
		const { material_id_list } = ctx.request.body
		if (!material_id_list || !material_id_list.length) {
			ctx.body = {
				code: 4894,
				message: 'material_id_list不能为空'
			}
			return
		}
		const res = await searchMaterial({
			material_id_list,
			access_token: ctx.headers.access_token
		})
		ctx.body = res
	}
	static async getCapInfo(ctx) {
		const res = await getCapInfo({
			access_token: ctx.headers.access_token
		})
		ctx.body = res
	}
	static async batchUploadVideoAsyncRouter(ctx) {
		// console.log('ctx', ctx.query.body)
		// 加入查询容量信息
		// 设置完成后需要删除素材 在编辑处
		const access_token = ctx.headers.access_token
		const { url } = ctx.request.body
		const getDirectory = await searchFolder({
			order_by: 0,
			page_num: 1,
			page_size: 1,
			name: '抖羚羊视频',
			parent_folder_id: 0,
			operate_status: 1,
			access_token,
		})
		let folder_id = 0
		if (getDirectory.code === 10000 && !getDirectory.data.total) {
			const addFlob = await createFolder({
				parent_folder_id: 0,
				name: '抖羚羊视频',
				type: 0,
				access_token,
			})
			if (addFlob.code === 10000) {
				folder_id = addFlob.data.folder_id
			}
		} else {
			folder_id = getDirectory.data.folder_info_list[0].folder_id
		}
		const request_id = Math.random().toString(36).substring(2, 32)
		// console.log('request_id', request_id)
		const batchData = {
			materials: [{
				request_id,
				folder_id,
				name: request_id + '.mp4',
				url,
				material_type: 'video',

			}],
			access_token,
		}
		const res = await batchUploadVideoAsync(batchData)
		function getVidDeteail() {
			return new Promise((resolve, reject) => {
				const getVidDeteail = setInterval(async () => {
					const getUpDataShear = await queryMaterialDetail({
						material_id: res.data.success_map[request_id].MaterialId,
						access_token,
					})
					if (getUpDataShear.data.material_info.video_info.vid) {
						clearInterval(getVidDeteail)
						resolve(getUpDataShear.data.material_info.video_info)
						return
					}
					if (getUpDataShear.data.material_info.audit_status === 4) {
						clearInterval(getVidDeteail)
						resolve(getUpDataShear.data.material_info.video_info)
						return
					}
				}, 300)
			})
		}
		const getVidData = await getVidDeteail()
		// console.log('getUpDataShear.data.material_info.video_info', getUpDataShear.data.material_info.video_info)
		ctx.body = getVidData
	}
	static async mGetPlayInfo(ctx) {
		// ctx.verifyParams({
		// 	vid: { type: 'string', required: true }
		// })
		const { vid } = ctx.request.body
		if (!vid) {
			ctx.body = {
				code: 4894,
				message: 'vid不能为空'
			}
			return
		}
		const res = await getVideoDetail({
			vid_list: vid,
			access_token: ctx.headers.access_token
		})
		ctx.body = res
	}
	static async deleteAllFolder(ctx) {
		const { access_token } = ctx.headers
		const upconter = await deleteMaterial({
			access_token,
			folder_id_list: ['0', '1', '2', '-1'], //['201', '202', '203', '204', '0'],
			operate_type: 'delete_forever',
			material_type_list: ["photo", "video"]
		})
		console.log('upconter', upconter)
		return ctx.body = '已提交'
	}
}


export default MaterialController
